<?php

class DocumentService extends BaseService {

	/**
	 * 获取内容列表
	 * @param  [id] $categoryId [栏目id]
	 * @return [array]             [栏目id、栏目名、文档内容]
	 */
	public static function index($categoryId){
		$category 	= Category::find($categoryId);
		if(!isset($category)){
			return CMSException::$noCategoryErr;
		}

		$modelId 	= $category['model_id'];
		$tableName  = Model::where('id', $modelId)->pluck('table_name');
		$tableName  = studly_case($tableName);
		if(!isset($tableName)){
			return CMSException::$noModelErr;
		}

		$document  = new $tableName;
		$documents = $document::all();
		// $documents = DB::table($tableName)->get();
		$documents = array('category' => $category, 'documents' => $documents);
		return $documents;
	}

	/**
	 *  进入文档增加页面
	 * @param  [int] $categoryId [栏目id]
	 * @return [array]             [栏目信息，模板字段信息]
	 */
	public static function create($categoryId){

		$category = Category::find($categoryId);
		if(!isset($category)){
			return CMSException::$noCategoryErr;
		}

		$modelId = $category['model_id'];
		$fields  = Field::where('model_id', $modelId)->get();
		if(!isset($fields)){
			return CMSException::$noFieldErr;
		}

		$document = array('category' => $category, 'fields' => $fields);
		return $document;
	}

	/**
	 * 增加文档
	 * @param [idt] $categoryId [栏目id]
	 */
	public static function add($categoryId){
		try{
			DB::transaction(function() use ($categoryId){
				$modelId = Category::where('id', $categoryId)->pluck('model_id');
				if(!isset($modelId)){
					return CMSException::$noCategoryErr;
				}
				$model = Model::find($modelId);
				if(!isset($model)){
					return CMSException::$noModelErr;
				}
				$tableName  = $model['table_name'];
				$fields 	= Field::where('model_id', $modelId)->get();
				
				//增加文档
				$model 		= new $tableName;
				//判断字段是否属于该模型
				foreach ($_POST as $key => $value) {
					$model->category_id = $categoryId;
					//判断字段类型与内容是否符合
					foreach ($fields as $number => $field) {

						if($key==$field['field_name']){
							
							//数据类型为string
							if($field['field_type']=='string'){
								if(is_string($value)){
									if(strlen($value)>255){
										throw new Exception('字符串长度超过255',10000);
									}
									$model->$key=$value;
								}
							}
							
							//数据类型为integer
							if($field['field_type']=='integer'){
								self::validateInput(
											array('value' => $value),
											array('value' => 'integer')
										);
								$model->$key=$value;
							}
							
							//数据类型为boolean
							if($field['field_type']=='boolean'){
								self::validateInput(
											array('value' => $value),
											array('value' => 'boolean')
										);
								$model->$key=$value;
							}
							
							//数据类型为date
							if($field['field_type']=='date'){
								if(date('Y-m-d', strtotime($value)) === $value){
									$model->$key=$value;
								}
								throw new Exception('时间错误');
							}
							
							//数据类型为dateTime,timestamp
							if($field['field_type']=='dateTime' || $field['field_type']=='timestamp'){
								if(date('Y-m-d H:i:s', strtotime($value)) === $value){
									$model->$key=$value;
								}
								throw new Exception('时间错误');
							}

							//time
							if($field['field_type']=='time'){
								if(date('H:i:s', strtotime($value)) === $value){
									$model->$key=$value;
								}
								throw new Exception('时间错误');
							}
							
							//数据类型为数字
							if($field['field_type']=='double' || $field['field_type']=='decimal'){
								self::validateInput(
											array('value' => $value),
											array('value' => 'numeric')
										);
								$model->$key=$value;
							}
							
							//数据类型为text,longText
							if($field['field_type']=='text' || $field['field_type']=='longText'){
								if(is_string($value)){
									$model->$key=$value;
								}
							}
						}
						$model->save();
					}
				}
			});
		}catch(Exception $e){
			dd($e->getMessage());
			return $e->getMessage();//CMSException::$addDocumentErr;
		}
		return CMSException::$ok;

	}

	/**
	 * 进入文档更改页面
	 * @param  [int] $id         [文档id]
	 * @param  [int] $categoryId [栏目id]
	 * @return [array]             [文档信息，模板字段]
	 */
	public static function show($id, $categoryId){
		$modelId = Category::where('id', $categoryId)->pluck('model_id');
		if(!isset($modelId)){
			return CMSException::$noCategoryErr;
		}
		$tableName = Model::where('id', $modelId)->pluck('table_name');

		$model      = new $tableName;
		$documents  = $model::find($id);
		if(!isset($documents)){
			return CMSException::$noDocumentErr;
		}
		$fields = Field::where('model_id', $modelId)->lists('field_name');
		if(!isset($fields)){
			return CMSException::$noFieldErr;
		}
		$documents = array('documents' => $documents, 'fields' => $fields);
		return $documents;
	}

	/**
	 * 更新文档数据
	 * @param  [int] $id         [文档id]
	 * @param  [int] $categoryId [栏目id]
	 */
	public static function update($id, $categoryId){
		try{
			DB::transaction(function() use ($id, $categoryId){
				$modelId = Category::where('id', $categoryId)->pluck('model_id');
				if(!isset($modelId)){
					return CMSException::$noCategoryErr;
				}
				$model = Model::find($modelId);
				if(!isset($model)){
					return CMSException::$noModelErr;
				}
				$tableName  = $model['table_name'];
				$fields     = Field::where('model_id', $modelId)->get();
				if(!isset($fields)){
					return CMSException::$noFieldErr;
				}
				$model = $tableName::where('id', $id)->first();
				if(!isset($model)){
					return CMSException::$noModelErr;
				}
				//判断字段是否属于该模型
				foreach ($_POST as $key => $value) {
					$model->category_id = $categoryId;
					//判断字段类型与内容是否符合
					foreach ($fields as $number => $field) {
						if($key==$field['field_name']){
							
							//数据类型为string
							if($field['field_type']=='string'){
								if(is_string($value)){
									if(strlen($value)>255){
										throw new Exception('字符串长度超过255',10000);
									}
									$model->$key=$value;
								}
							}
							
							//数据类型为integer
							if($field['field_type']=='integer'){
								self::validateInput(
											array('value' => $value),
											array('value' => 'integer')
										);
								$model->$key=$value;
							}
							
							//数据类型为boolean
							if($field['field_type']=='boolean'){
								self::validateInput(
											array('value' => $value),
											array('value' => 'boolean')
										);
								$model->$key=$value;
							}
							
							//数据类型为date
							if($field['field_type']=='date'){
								if(date('Y-m-d', strtotime($value)) === $value){
									$model->$key=$value;
								}
								throw new Exception('时间错误');
							}
							
							//数据类型为dateTime,timestamp
							if($field['field_type']=='dateTime' || $field['field_type']=='timestamp'){
								if(date('Y-m-d H:i:s', strtotime($value)) === $value){
									$model->$key=$value;
								}
								throw new Exception('时间错误');
							}

							//time
							if($field['field_type']=='time'){
								if(date('H:i:s', strtotime($value)) === $value){
									$model->$key=$value;
								}
								throw new Exception('时间错误');
							}
							
							//数据类型为数字
							if($field['field_type']=='double' || $field['field_type']=='decimal'){
								self::validateInput(
											array('value' => $value),
											array('value' => 'numeric')
										);
								$model->$key=$value;
							}
							
							//数据类型为text,longText
							if($field['field_type']=='text' || $field['field_type']=='longText'){
								if(is_string($value)){
									$model->$key=$value;
								}
							}
						}
						$model->save();
					}
				}
			});
		}catch(Exception $e){
			return $e->getMessage();//CMSException::$updateDocumentErr;
		}
		return CMSException::$ok;
	}

	/**
	 * 删除文档
	 * @param  [int] $id         [文档id]
	 * @param  [int] $categoryId [栏目id]
	 */
	public static function delete($id, $categoryId){
		try{
			$modelId = Category::where('id', $categoryId)->pluck('model_id');
			if(!isset($modelId)){
				return CMSException::$noCategoryErr;
			}
			$model = Model::find($modelId);
			if(!isset($model)){
				return CMSException::$noModelErr;
			}
			$modelName  = ucfirst($model['table_name']);
			$model      = $modelName::where('id', $id)->first();
			if(!isset($model)){
				return CMSException::$noModelErr;
			}
			$model->delete();
		}catch (Exception $e){
			return $e;
		}
		return CMSException::$ok;
	}

}